home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c13 / BadTechnique.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  9.1 KB  |  292 lines

  1. //: BadTechnique.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // It's possible to override components this way,
  50. // but the listener approach is much better, so
  51. // why would you?
  52. import java.awt.*;
  53. import java.awt.event.*;
  54.  
  55. class Display {
  56.   public static final int
  57.     EVENT = 0, COMPONENT = 1,
  58.     MOUSE = 2, MOUSE_MOVE = 3,
  59.     FOCUS = 4, KEY = 5, ACTION = 6,
  60.     LAST = 7;
  61.   public String[] evnt;
  62.   Display() {
  63.     evnt = new String[LAST];
  64.     for(int i = 0; i < LAST; i++)
  65.       evnt[i] = new String();
  66.   }
  67.   public void show(Graphics g) {
  68.     for(int i = 0; i < LAST; i++)
  69.       g.drawString(evnt[i], 0, 10 * i + 10);
  70.   }
  71. }
  72.  
  73. class EnabledPanel extends Panel {
  74.   Color c;
  75.   int id;
  76.   Display display = new Display();
  77.   public EnabledPanel(int i, Color mc) {
  78.     id = i;
  79.     c = mc;
  80.     setLayout(new BorderLayout());
  81.     add(new MyButton(), BorderLayout.SOUTH);
  82.     // Type checking is lost. You can enable and
  83.     // process events that the component doesn't
  84.     // capture:
  85.     enableEvents(
  86.       // Panel doesn't handle these:
  87.       AWTEvent.ACTION_EVENT_MASK |
  88.       AWTEvent.ADJUSTMENT_EVENT_MASK |
  89.       AWTEvent.ITEM_EVENT_MASK |
  90.       AWTEvent.TEXT_EVENT_MASK |
  91.       AWTEvent.WINDOW_EVENT_MASK |
  92.       // Panel can handle these:
  93.       AWTEvent.COMPONENT_EVENT_MASK |
  94.       AWTEvent.FOCUS_EVENT_MASK |
  95.       AWTEvent.KEY_EVENT_MASK |
  96.       AWTEvent.MOUSE_EVENT_MASK |
  97.       AWTEvent.MOUSE_MOTION_EVENT_MASK |
  98.       AWTEvent.CONTAINER_EVENT_MASK);
  99.       // You can enable an event without
  100.       // overriding its process method.
  101.   }
  102.   // To eliminate flicker:
  103.   public void update(Graphics g) {
  104.     paint(g);
  105.   }
  106.   public void paint(Graphics  g) {
  107.     g.setColor(c);
  108.     Dimension s = getSize();
  109.     g.fillRect(0, 0, s.width, s.height);
  110.     g.setColor(Color.black);
  111.     display.show(g);
  112.   }
  113.   public void processEvent(AWTEvent e) {
  114.     display.evnt[Display.EVENT]= e.toString();
  115.     repaint();
  116.     super.processEvent(e);
  117.   }
  118.   public void
  119.   processComponentEvent(ComponentEvent e) {
  120.     switch(e.getID()) {
  121.       case ComponentEvent.COMPONENT_MOVED:
  122.         display.evnt[Display.COMPONENT] = 
  123.           "Component moved";
  124.         break;
  125.       case ComponentEvent.COMPONENT_RESIZED:
  126.         display.evnt[Display.COMPONENT] = 
  127.           "Component resized";
  128.         break;
  129.       case ComponentEvent.COMPONENT_HIDDEN:
  130.         display.evnt[Display.COMPONENT] = 
  131.           "Component hidden";
  132.         break;
  133.       case ComponentEvent.COMPONENT_SHOWN:
  134.         display.evnt[Display.COMPONENT] = 
  135.           "Component shown";
  136.         break;
  137.       default:
  138.     }
  139.     repaint();
  140.     // Must always remember to call the "super"
  141.     // version of whatever you override:
  142.     super.processComponentEvent(e);
  143.   }
  144.   public void processFocusEvent(FocusEvent e) {
  145.     switch(e.getID()) {
  146.       case FocusEvent.FOCUS_GAINED:
  147.         display.evnt[Display.FOCUS] = 
  148.           "FOCUS gained";
  149.         break;
  150.       case FocusEvent.FOCUS_LOST:
  151.         display.evnt[Display.FOCUS] = 
  152.           "FOCUS lost";
  153.         break;
  154.       default:
  155.     }
  156.     repaint();
  157.     super.processFocusEvent(e);
  158.   }
  159.   public void processKeyEvent(KeyEvent e) {
  160.     switch(e.getID()) {
  161.       case KeyEvent.KEY_PRESSED:
  162.         display.evnt[Display.KEY] = 
  163.           "KEY pressed: "; 
  164.         break;
  165.       case KeyEvent.KEY_RELEASED:
  166.         display.evnt[Display.KEY] = 
  167.           "KEY released: "; 
  168.         break;
  169.       case KeyEvent.KEY_TYPED:
  170.         display.evnt[Display.KEY] = 
  171.           "KEY typed: ";
  172.         break;
  173.       default:
  174.     }
  175.     int code = e.getKeyCode();
  176.     display.evnt[Display.KEY] += 
  177.       KeyEvent.getKeyText(code);
  178.     repaint();
  179.     super.processKeyEvent(e);
  180.   }
  181.   public void processMouseEvent(MouseEvent e) {
  182.     switch(e.getID()) {
  183.       case MouseEvent.MOUSE_CLICKED:
  184.         requestFocus(); // Get FOCUS on click
  185.         display.evnt[Display.MOUSE] = 
  186.           "MOUSE clicked";
  187.         break;
  188.       case MouseEvent.MOUSE_PRESSED:
  189.         display.evnt[Display.MOUSE] = 
  190.           "MOUSE pressed";
  191.         break;
  192.       case MouseEvent.MOUSE_RELEASED:
  193.         display.evnt[Display.MOUSE] = 
  194.           "MOUSE released";
  195.         break;
  196.       case MouseEvent.MOUSE_ENTERED: 
  197.         display.evnt[Display.MOUSE] = 
  198.           "MOUSE entered";
  199.         break;
  200.       case MouseEvent.MOUSE_EXITED: 
  201.         display.evnt[Display.MOUSE] = 
  202.           "MOUSE exited";
  203.         break;
  204.       default:
  205.     }
  206.     display.evnt[Display.MOUSE] += 
  207.       ", x = " + e.getX() + 
  208.       ", y = " + e.getY();
  209.     repaint();
  210.     super.processMouseEvent(e);
  211.   }
  212.   public void
  213.   processMouseMotionEvent(MouseEvent e) {
  214.     switch(e.getID()) {
  215.       case MouseEvent.MOUSE_DRAGGED:
  216.         display.evnt[Display.MOUSE_MOVE] = 
  217.           "MOUSE dragged";
  218.         break;
  219.       case MouseEvent.MOUSE_MOVED:
  220.         display.evnt[Display.MOUSE_MOVE] = 
  221.           "MOUSE moved";
  222.         break;
  223.       default:
  224.     }
  225.     display.evnt[Display.MOUSE_MOVE] += 
  226.       ", x = " + e.getX() + 
  227.       ", y = " + e.getY();
  228.     repaint();
  229.     super.processMouseMotionEvent(e);
  230.   }
  231. }
  232.  
  233. class MyButton extends Button {
  234.   int clickCounter;
  235.   String label = "";
  236.   public MyButton() {
  237.     enableEvents(AWTEvent.ACTION_EVENT_MASK);
  238.   }
  239.   public void paint(Graphics g) {
  240.     g.setColor(Color.green);
  241.     Dimension s = getSize();
  242.     g.fillRect(0, 0, s.width, s.height);
  243.     g.setColor(Color.black);
  244.     g.drawRect(0, 0, s.width - 1, s.height - 1);
  245.     drawLabel(g);
  246.   }
  247.   private void drawLabel(Graphics g) {
  248.     FontMetrics fm = g.getFontMetrics();
  249.     int width = fm.stringWidth(label);
  250.     int height = fm.getHeight();
  251.     int ascent = fm.getAscent();
  252.     int leading = fm.getLeading();
  253.     int horizMargin = 
  254.       (getSize().width - width)/2;
  255.     int verMargin = 
  256.       (getSize().height - height)/2;
  257.     g.setColor(Color.red);
  258.     g.drawString(label, horizMargin, 
  259.                  verMargin + ascent + leading);
  260.   }
  261.   public void processActionEvent(ActionEvent e) {
  262.     clickCounter++;
  263.     label = "click #" + clickCounter +
  264.       " " + e.toString();
  265.     repaint();
  266.     super.processActionEvent(e);
  267.   }
  268. }
  269.   
  270. public class BadTechnique extends Frame {
  271.   BadTechnique() {
  272.     setLayout(new GridLayout(2,2));
  273.     add(new EnabledPanel(1, Color.cyan));
  274.     add(new EnabledPanel(2, Color.lightGray));
  275.     add(new EnabledPanel(3, Color.yellow));
  276.     // You can also do it for Windows:
  277.     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  278.   }
  279.   public void processWindowEvent(WindowEvent e) {
  280.     System.out.println(e);
  281.     if(e.getID() == WindowEvent.WINDOW_CLOSING) {
  282.       System.out.println("Window Closing");
  283.       System.exit(0);
  284.     }
  285.   }
  286.   public static void main(String[] args) {
  287.     Frame f = new BadTechnique();
  288.     f.setTitle("Bad Technique");
  289.     f.setSize(700,700);
  290.     f.setVisible(true);
  291.   }
  292. } ///:~